package com.boot.unity.api.modules.province.controller;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.modules.listener.StudentSyncService;
import com.boot.unity.api.modules.province.entity.TrainingTime;
import com.boot.unity.api.modules.province.result.Result;
import com.boot.unity.api.modules.province.service.TrainingTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//@Api(tags = "省平台-培训学时写入接口")
//@RestController
//@RequestMapping("/trainingTime")
@RequiredArgsConstructor
public class TrainingTimeController {
    private final TrainingTimeService trainingTimeService;
    private final StudentSyncService studentSyncService;


    // 省平台
    @Value("${province.secret}")
    private String secret;


    //计时平台推送的学时数据
    @Value("${province.secret2}")
    private String secret2;

    @PostMapping("stopTime")
    public Result stopTime(@RequestBody JSONObject json){
        String cardNo = json.getString("cardNo");
        String token = json.getString("token");
        if (StringUtils.isEmpty(cardNo)) {
            return Result.fail("证件号码不能为空！");
        }
        if (StringUtils.isEmpty(token)) {
            return Result.fail("token不能为空！");
        }
        System.out.println("cardNo:::"+cardNo);
        System.out.println("token:::"+token);
        String result = "0";
        if (secret2.equals(token)){
            result = studentSyncService.studentStopSync(cardNo);
        }else{
            return Result.fail("token不正确！");
        }

        return Result.success("1","");
    }



    @PostMapping("changeTrainTypeAction")
    public Result changeTrainTypeAction(@RequestBody JSONObject json){
        String cardNo = json.getString("cardNo");
        String token = json.getString("token");
        String trainType = json.getString("trainType");
        if (StringUtils.isEmpty(cardNo)) {
            return Result.fail("证件号码不能为空！");
        }
        if (StringUtils.isEmpty(token)) {
            return Result.fail("token不能为空！");
        }
        if (StringUtils.isEmpty(trainType)) {
            return Result.fail("车型不能为空！");
        }
        System.out.println("cardNo:::"+cardNo);
        System.out.println("token:::"+token);
        System.out.println("trainType:::"+trainType);
        String result = "0";
        if (secret2.equals(token)){
            result = studentSyncService.studentChangeTrainTypeSync(cardNo,trainType);
        }else{
            return Result.fail("token不正确！");
        }

        return Result.success("1","");
    }



    @ApiOperation(value = "写入")
    @PostMapping
    public Result save(@RequestBody TrainingTime trainingTime, HttpServletRequest request) {
        String secret_ = request.getHeader("secret");
        if (StringUtils.isBlank(secret_)){
            return Result.fail("密钥不能为空！");
        }
        // 省平台的数据
        if (secret.equals(secret_)){
            trainingTime.setOrigin(0);
        // 省交通运输部数据
        }else if (secret2.equals(secret_)){
            trainingTime.setOrigin(1);
        }else {
            return Result.fail("密钥不正确");
        }
        return trainingTimeService.save(trainingTime);
    }

    @ApiOperation(value = "查询最新10条数据(测试用)")
    @GetMapping
    public Result list(HttpServletRequest request){
        String secret_ = request.getHeader("secret");
        if (StringUtils.isBlank(secret_)){
            return Result.fail("密钥不能为空！");
        }
        if (!secret.equals(secret_)){
            return Result.fail("密钥不正确");
        }
        return trainingTimeService.list();
    }

//    @GetMapping("/{id}")
//    @ApiOperation(value = "根据id获取")
//    public TrainingTime get(@PathVariable Long id) {
//        return trainingTimeDao.getById(id);
//    }
//
//    @PutMapping
//    @ApiOperation(value = "修改")
//    public TrainingTime update(@RequestBody TrainingTime trainingTime) {
//        trainingTimeDao.update(trainingTime);
//
//        return trainingTime;
//    }
//
//    @GetMapping
//    @ApiOperation(value = "列表")
//    public PageTableResponse list(PageTableRequest tableRequest) {
//        return new PageTableHandler(
//            request -> trainingTimeDao.count(request.getParams()),
//            request -> trainingTimeDao.list(request.getParams(), request.getOffset(), request.getLimit())
//        ).handle(tableRequest);
//    }
//
//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "删除")
//    public void delete(@PathVariable Long id) {
//        trainingTimeDao.delete(id);
//    }
}
